package com.market.dao.jpa;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;

import org.springframework.stereotype.Repository;

import com.market.dao.PictureDao;
import com.market.domain.OrderDetail;
import com.market.domain.Picture;

@Repository("pictureDao")
public class PictureDaoJpa extends GenericDaoJpa<Picture, Long> implements
		PictureDao {

	public PictureDaoJpa() {
		super(Picture.class);
	}

	public List<Picture> findAllRecycled() {
		String sql = "select * from picture where isRecycled = 1";
		Query q = this.em.createNativeQuery(sql, Picture.class);
		List<Picture> pictures = q.getResultList();
		return pictures;
	}

	public List<Picture> findAllNotRecycled() {
		String sql = "select * from picture where isRecycled = 0";
		Query q = this.em.createNativeQuery(sql, Picture.class);
		List<Picture> pictures = q.getResultList();
		return pictures;
	}

	/**
	 * This method override super class save method, because save picture have
	 * to upload picture too.
	 * 
	 */
	@Override
	public Picture save(Picture picture) {

		String fileName;
		if (picture.getIsNew() == true) { // If this is a new picture, have to
											// persist it to get the id for
											// generating path and fileName
			picture = super.persist(picture);

		}

		if (picture.getUploadedFile() != null) { // means need to upload picture
			fileName = picture.getId() + "_"
					+ picture.getUploadedFile().getFileName();
			picture.setFileName(fileName);
			picture.setPath(picture.generatePath());

			super.uploadFile(Picture.getBaseDir() + "/" + picture.getPath()
					+ "/", picture.getFileName(), picture.getUploadedFile());
		}
		picture = super.merge(picture);
		return picture;
	}

	public List<Picture> findAllHomePagePictures() {
		String sql = "select * from picture where isHomePage = 1 order by recommendIndex desc";
		Query q = this.em.createNativeQuery(sql, Picture.class);
		List<Picture> pictures = q.getResultList();
		return pictures;
	}
	
	
	

	public List<Picture> findPictureByYear(String year) {
		String sql = "select * from picture where year(takenDate) = '" + year + "' order by recommendIndex desc,uploadedDate desc";
		Query q = this.em.createNativeQuery(sql, Picture.class);
		List<Picture> pictures = q.getResultList();
		return pictures;
	}

	public void remove(Picture picture) {
		if (this.findById(picture.getId()) == null) {
			log.debug(this.getClass() + " Picture with id " + picture.getId()
					+ " is not found, can't be removed");
		} else {
			log.debug(this.getClass() + " Picture with id " + picture.getId()
					+ " is found, will be removed");
			this.removeById(picture.getId());
		}
		this.deleteFile(picture);

	}

	private void deleteFile(Picture picture) {
		File file = new File(FacesContext.getCurrentInstance()
				.getExternalContext().getRealPath("")
				+ "/"
				+ Picture.getBaseDir()
				+ "/"
				+ picture.getPath()
				+ "/"
				+ picture.getFileName());
		if (file.exists()) {
			file.delete();
		}

	}

}
